﻿using Abp.AspNetCore.Mvc.Auditing;
using Microsoft.AspNetCore.Http;
using System.Linq;
using AbpProjectTemplate.Extensions;

namespace AbpProjectTemplate.Web.Auditing
{
    public class AppHttpContextClientInfoProvider : HttpContextClientInfoProvider
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        public AppHttpContextClientInfoProvider(IHttpContextAccessor httpContextAccessor) 
            : base(httpContextAccessor)
        {
            this._httpContextAccessor = httpContextAccessor;
        }

        protected override string GetClientIpAddress()
        {
            var httpContext = this._httpContextAccessor.HttpContext;

            var ip = httpContext?.Request?.Headers["X-Forwarded-For"].FirstOrDefault();
            if (!ip.HasValue())
                ip = base.GetClientIpAddress();
            return ip;
        }
    }
}
